<?php

namespace App\Models\Finance;

use App\Models\User;
use App\Observers\Finance\WithdrawObserver;
use Dcat\Admin\Admin;
use Dcat\Admin\Traits\HasDateTimeFormatter;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Log;

class Withdraw extends Model
{
	use HasDateTimeFormatter;
    protected $table = 'yida_withdraws';

    /**
     * 状态：审核成功
     */
    const STATUS_SUCCESS = 'Success';

    /**
     * 状态：待审核
     */
    const STATUS_UNDER_REVIEW = 'UnderReview';


    /**
     * 状态：失败
     */
    const STATUS_FAILURE = 'Failure';

    public static function booted()
    {
        parent::booted(); // TODO: Change the autogenerated stub

        static::created(function ($withdraws){
            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_insert'=>$withdraws->toArray()];
            Log::channel("LaravelInsert")->error("添加操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"insert",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "添加操作",
                'table' => "yida_withdraws"
            ]);
        });
        static::updated(function ($withdraws){
            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_update'=>$withdraws->toArray()];
            Log::channel("LaravelUpdate")->error("更新操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"update",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "更新操作",
                'table' => "yida_withdraws"
            ]);
        });
        static::deleted(function ($withdraws){

            $adminId = isset(Admin::user()->id) ? Admin::user()->id : 0;
            $data['admin_id'] = $adminId;
            $data['ip'] = request()->ip();
            $data['url'] = request()->url();
            $data['message'] = ['before_delete'=>$withdraws->toArray()];
            Log::channel("LaravelDelete")->error("删除操作",[
                'function'=>__CLASS__.'->'.__FUNCTION__.'()',
                'code_line'=>__LINE__,
                'action'=>"delete",
                'message'=>json_encode($data,JSON_UNESCAPED_UNICODE),
                'title' => "删除操作",
                'table' => "yida_withdraws"
            ]);
        });

    }

    public function user(){
        return $this->belongsTo(User::class,'user_id');
    }

    public function wallet(){
        return $this->belongsTo(Wallet::class);
    }

}
